<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
define('PER_PAGE', 6);
class Restaurant extends Front_Controller {
    function __construct()
    {
        parent::__construct();
        //load the model
        $this->load->model('restaurant/restaurant_model');
    }

    function index(){
		$pref = ($this->uri->segment(2)) ? $this->uri->segment(2) : "ca-nuoc";
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
        $data['page'] = $page;
        $data['limit'] = PER_PAGE;
		
        $filters = array();
		if($pref != 'ca-nuoc')
		{
			$this->session->set_userdata('tinh_thanh', $pref);
			$filters['pref_alias'] = $pref;
		}

        $this->load->library('jquery_pagination');
        $config = array();
        $config["base_url"] = base_url() . "tinh-thanh/" . $pref . "/";
        $config["total_rows"] = $this->restaurant_model->get_restaurants($filters, true);
        $config["per_page"] = PER_PAGE;
        $config['cur_page'] = $page;
        $config = $this->_get_cofig_paging($config);
        $this->jquery_pagination->initialize($config);
        $data['pagination'] = $this->jquery_pagination->create_links();

        //$data['data'] = $this->restaurant_model->get_restaurants($filters);
		$this->smarty->assign('tinh_thanh',  $this->session->userdata('tinh_thanh'));
        $this->smarty->assign('data', $data);
        $this->smarty->display('restaurant_list');
    }

    function detail(){
        $alias = $this->uri->segment(2);
        $data = $this->restaurant_model->get_restaurant_by_alias($alias);
        if(!$data){
            $this->_redirect(base_url());
        }
        $images = array($data['image1'], $data['image2'], $data['image3'], $data['image4'], $data['image5']);

        $this->smarty->assign('images', $images);
        $this->smarty->assign('data', $data);
        $this->smarty->display('restaurant_detail');
    }
	
	public function ajax_search() {
        $string = $this->input->post('search_ajax');
        if(!empty($string)) {
        	$filters['search_ajax'] = $string;
        	$filters['limit'] = 10;
            $result = $this->restaurant_model->get_restaurants($filters);
        }
    }
}
